﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace ExcelDemo.Handle
{
    public class CaclHelper
    {
        public static DataTable GetCaclScoreDt(DataTable scoreDt, DataTable ruleDt)
        {
            List<string> sss = GetLevelAndScore(ruleDt, 26, "地理");
            DataTable dt = new DataTable();
            Dictionary<string, int> dictcolum = new Dictionary<string, int>();
            if (scoreDt != null && scoreDt.Rows.Count > 0)
            {

                dictcolum = GetColumnsByDataTable(scoreDt);

                // dt.Columns.AddRange(dictcolum.Keys.ToArray());
                int index = 0;

                //数据起始行
                int startIndex = 1;
                for (int i = startIndex; i < scoreDt.Rows.Count - startIndex; i++)
                {
                    DataRow dw = scoreDt.Rows[i];
                    string subName = scoreDt.Rows[i][0].ToString();
                    List<string> fullSubName = GetFullSubName(subName);
                    foreach (var item in fullSubName)
                    {
                        dictcolum.TryGetValue(item, out index);
                        float Score = float.Parse(Math.Round(float.Parse(dw[index].ToString()), 0).ToString());
                        List<string> resultScore = GetLevelAndScore(ruleDt, Score, item);
                        dw[index + 1] = resultScore[1];
                        dw[index + 2] = resultScore[0];
                    }
                }
                dt = GetNewDataTable(scoreDt, "", "");
            }
            return dt;
        }

        #region 根据datatable获得列名     
        /// <summary>
        /// 根据datatable获得列名
        /// </summary>
        /// <param name="dt">表对象</param>
        /// <returns>返回结果的数据列数组</returns>
        public static Dictionary<string, int> GetColumnsByDataTable(DataTable dt)
        {
            Dictionary<string, int> strColumns = null;


            if (dt.Columns.Count > 0)
            {
                int columnNum = 0;
                columnNum = dt.Columns.Count;
                strColumns = new Dictionary<string, int>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    strColumns.Add(dt.Columns[i].ColumnName, i);
                }
            }

            return strColumns;
        }
        #endregion


        public static List<string> GetFullSubName(string subName)
        {
            List<string> fullSubName = new List<string>();
            //if (subName.Contains("历"))
            //{
            //    fullSubName.Add("历史");
            //}
            if (subName.Contains("生"))
            {
                fullSubName.Add("生物");
            }
            if (subName.Contains("地"))
            {
                fullSubName.Add("地理");
            }
            if (subName.Contains("政"))
            {
                fullSubName.Add("政治");
            }
            if (subName.Contains("化"))
            {
                fullSubName.Add("化学");
            }
            if (subName.Contains("理"))
            {
                fullSubName.Add("物理");
            }
            return fullSubName;
        }


        public static List<string> GetLevelAndScore(DataTable ruleDt, float score, string subName)
        {
            List<string> result = new List<string>();
            string level = "";
            //原始区间范围
            string[] suoshuqjscore = { };
            //等级区间范围
            string[] Levelqjscore = { };
            for (int i = 0; i < ruleDt.Rows.Count; i++)
            {
                //计算等级
                if (i == 0)
                {
                    for (int j = 1; j < ruleDt.Columns.Count; j++)
                    {
                        string qujian = ruleDt.Rows[i][j].ToString();
                        float minscore = float.Parse(ruleDt.Rows[i][5].ToString().Split('-')[1]);
                        string[] qjscore = qujian.Split('-');
                        if (score > float.Parse(qjscore[0]))
                        {
                            continue;
                        }
                        else if (score < float.Parse(qjscore[1]))
                        {
                            if (score < minscore)
                            {
                                suoshuqjscore = qjscore;
                                level = "E";
                                result.Add(level);
                                break;
                            }
                            continue;
                        }
                        else if (score <= float.Parse(qjscore[0]) && score >= float.Parse(qjscore[1]))
                        {
                            suoshuqjscore = qjscore;
                            switch (j)
                            {
                                case 1: level = "A"; break;
                                case 2: level = "B"; break;
                                case 3: level = "C"; break;
                                case 4: level = "D"; break;
                                case 5: level = "E"; break;
                            }
                            result.Add(level);
                            break;
                        }
                    }
                }
                else
                {
                    //计算赋分
                    string comparesubname = ruleDt.Rows[i][0].ToString();
                    if (subName.Equals(comparesubname))
                    {
                        for (int j = 1; j < ruleDt.Columns.Count; j++)
                        {
                            int columNum = 0;
                            if (level == "A")
                            {
                                columNum = j;
                            }
                            if (level == "B")
                            {
                                columNum = j + 1;
                            }

                            if (level == "C")
                            {
                                columNum = j + 2;
                            }

                            if (level == "D")
                            {
                                columNum = j + 3;
                            }
                            if (level == "E")
                            {
                                columNum = j + 4;
                            }
                            string qujian = ruleDt.Rows[i][columNum].ToString();
                            Levelqjscore = qujian.Split('-');
                            float levelScore = 0;
                            float firstval = float.Parse(suoshuqjscore[0]) - score;
                            float secondval = float.Parse(suoshuqjscore[0]) - float.Parse(suoshuqjscore[1]);

                            float leftcount = firstval / secondval;
                            levelScore =float.Parse(Math.Round((float.Parse(Levelqjscore[0]) + leftcount * float.Parse(Levelqjscore[1])) / (leftcount + 1), 1).ToString());
                            result.Add(levelScore.ToString());
                            break;
                        }
                    }
                }
            }
            return result;
        }


        /// 执行DataTable中的查询返回新的DataTable
        /// </summary>
        /// <param name="dt">源数据DataTable</param>
        /// <param name="condition">查询条件</param>
        /// <returns></returns>
        public static DataTable GetNewDataTable(DataTable dt, string condition, string sortstr)
        {
            DataTable newdt = new DataTable();
            newdt = dt.Clone();
            DataRow[] dr = dt.Select(condition, sortstr);
            for (int i = 0; i < dr.Length; i++)
            {
                newdt.ImportRow((DataRow)dr[i]);
            }
            return newdt;//返回的查询结果

        }

    }
}
